from jili.calc import calcor_base
def copydict(a):
    b={}
    for k ,v in a.items():
        b[k]=v
    return b
class hl_point_shape(calcor_base):
    def __init__(self, ta_arg={}):
        self.name = "hl_point_shape"
        self.group = "range"
        self.display_name = "hl_point_shape"
        self.output_names = ["prehigh", "prehightimekey", "prelow","prelowtimekey"]
        self.input_names = ["high", "low"]
        self.parameters = {"fx": "up"}
        self.output=dict(highlist=[],lowlist=[],prehigh={},prelow={},lasthigh={},lastlow={})
        self.ck=[]
        super().__init__(ta_arg)
        self.fx=self.parameters["fx"]
    def onbar(self, bar):
        input=self.getinputlist(bar)
        if self.ck:
            if self.fx=="up":
                if input[0]<self.ck[0] and input[1]<self.ck[1]:
                    self.fx="down"
                    self.ck=input
                    self.output["prehigh"]=copydict(self.output["lasthigh"])
                elif input[0]>self.ck[0] and input[1]<self.ck[1]:
                    self.ck[0]=input[0]
                    self.output["lasthigh"]["price"]=input[0]
                    self.output["lasthigh"]["timekey"]=bar["timekey"]
                elif input[0]<self.ck[0] and input[1]>self.ck[1]:
                    self.ck[1] = input[1]
                    self.output["lastlow"]["price"] = input[1]
                    self.output["lastlow"]["timekey"] = bar["timekey"]
                else:
                    self.ck=input
                    self.output["lasthigh"]["price"] = input[0]
                    self.output["lasthigh"]["timekey"] = bar["timekey"]
                    self.output["lastlow"]["price"] = input[1]
                    self.output["lastlow"]["timekey"] = bar["timekey"]
            else:
                if input[0]>self.ck[0] and input[1]>self.ck[1]:
                    self.fx="up"
                    self.ck=input
                    self.output["prelow"]=copydict(self.output["lastlow"])
                elif input[0]<self.ck[0] and input[1]>self.ck[1]:
                    self.ck[0] = input[0]
                    self.output["lasthigh"]["price"]=input[0]
                    self.output["lasthigh"]["timekey"]=bar["timekey"]
                elif input[0]>self.ck[0] and input[1]<self.ck[1]:
                    self.ck[1] = input[1]
                    self.output["lastlow"]["price"] = input[1]
                    self.output["lastlow"]["timekey"] = bar["timekey"]
                else:
                    self.ck = input
                    self.output["lasthigh"]["price"] = input[0]
                    self.output["lasthigh"]["timekey"] = bar["timekey"]
                    self.output["lastlow"]["price"] = input[1]
                    self.output["lastlow"]["timekey"] = bar["timekey"]
            t={}
            if self.output["prehigh"]:
                t[self.output_names[0]]=self.output["prehigh"]["price"]
                t[self.output_names[1]] = self.output["prehigh"]["timekey"]
            else:
                t[self.output_names[0]] = None
                t[self.output_names[1]] = None
            if self.output["prelow"]:
                t[self.output_names[2]]=self.output["prelow"]["price"]
                t[self.output_names[3]] = self.output["prelow"]["timekey"]
            else:
                t[self.output_names[2]] = None
                t[self.output_names[3]] = None
            return t
        else:
            self.ck=input
            t={}
            for k in self.output_names:
                t[k]=None
            return t